package com.chif;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;
/**
 * MessageListenerConcurrently消费者
 */
public class SomeConsumer {

    public static void main(String[] args) throws MQClientException {

        //顶一个pull消费者
        //DefaultLitePullConsumer consumer = new DefaultLitePullConsumer();

        //定义一个push消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("cg");
        consumer.setNamesrvAddr("192.168.10.100:9876");

        //指定从第一条消费开始消费
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

        //指定消费topic与tag
        consumer.subscribe("someTopic","*");

        //指定采用广播模式进行消费，默认为集群模式
        //consumer.setMessageModel(MessageModel.BROADCASTING);

        //注册消息监听器
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {

                //逐条消费消息
                for (MessageExt msg : msgs) {
                    System.out.println(msg);
                }

                //返回当前consumer消费的状态，消费成功
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        //开启消费者消费
        consumer.start();

    }
}
